# coding=utf-8

# author: Reggie
# time:   2019/08/26 18:02

import os, sys, time
import pika


# 接收处理消息的回调函数
def ConsumerCallback(channel, method, properties, body):
    # 如果未开启auto_ack
    print("Received %s" % body, method.delivery_tag)
    # 向生成者发送消费完毕的确认信息，然后生产者将此条消息同队列里剔除
    # channel.basic_ack(delivery_tag=method.delivery_tag)

def Main():
    credentials = pika.PlainCredentials("wangshuliang", "123")
    parameters = pika.ConnectionParameters(host="129.28.181.19",
                                           virtual_host='/',
                                           credentials=credentials)
    connection = pika.BlockingConnection(parameters)  # 连接 RabbitMQ

    channel = connection.channel()  # 创建频道

    queue = channel.queue_declare(queue='myhost', durable=True)  # 声明或创建队列

    # no_ack=True 开启自动确认，不然消费后的消息会一直留在队列里面
    # no_ack = no_manual_ack = auto_ack；不手动应答，开启自动应答模式
    channel.basic_consume(on_message_callback=ConsumerCallback, queue="myhost", auto_ack=True)
    print('Wait Message ...')

    channel.start_consuming()


if __name__ == '__main__':
    Main()
